package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {


    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;


    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Override
    public void saveOrUpdateVO(ApartmentSubmitVo apartmentSubmitVo) {
        //公寓配套ids
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        //公寓标签ids
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        //公寓杂费ids
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        //公寓图片集合
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();

        Long delApartmentId = apartmentSubmitVo.getId();
        if(delApartmentId!=null){//如果不为空是新增，需要先删除
            //根据公寓id删除公寓信息
            removeById(delApartmentId);
            //根据公寓id删除公寓配套列表
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                                                    .eq(ApartmentFacility::getApartmentId,delApartmentId));

            apartmentLabelService.remove((new LambdaQueryWrapper<ApartmentLabel>()
                                                    .eq(ApartmentLabel::getApartmentId, delApartmentId)));
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                                                    .eq(ApartmentFeeValue::getApartmentId, delApartmentId));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                                                    .eq(GraphInfo::getItemId, delApartmentId));


        }
        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());
        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());

        //存储apartmentInfo
        saveOrUpdate(apartmentSubmitVo);
        //定义最终apartmentId保证：以下问题
        //对有效最终变量的限制禁止访问动态变化的局部变量，捕获这些局部变量可能会引发并发问题。
        final Long apartmentId = apartmentSubmitVo.getId();


        //公寓配套新增
//        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
//        for (Long id : facilityInfoIds) {
//            apartmentFacilities.add(ApartmentFacility.builder().apartmentId(apartmentId).facilityId(id).build());
//        }
//        apartmentFacilityService.saveBatch(apartmentFacilities);

        apartmentFacilityService.saveBatch(facilityInfoIds.stream()
                .map(id -> ApartmentFacility.builder().apartmentId(apartmentId).facilityId(id).build())
                .collect(Collectors.toList()));

        //公寓标签新增
        apartmentLabelService.saveBatch(labelIds.stream()
                .map(id -> ApartmentLabel.builder().apartmentId(apartmentId).labelId(id).build())
                .collect(Collectors.toList()));



        //公寓杂费新增
        apartmentFeeValueService.saveBatch(feeValueIds.stream()
                .map(id -> ApartmentFeeValue.builder().apartmentId(apartmentId).feeValueId(id).build())
                .collect(Collectors.toList()));

        //公寓图片新增
        ArrayList<GraphInfo> graphInfoArrayList = new ArrayList<>();
        for (GraphVo gv: graphVoList) {//将视图Vo数据组装给graphInfo实体
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(gv.getName());
            graphInfo.setUrl(gv.getUrl());
            graphInfo.setItemId(apartmentId);
            graphInfo.setItemType(ItemType.APARTMENT);

            graphInfoArrayList.add(graphInfo);

        }
        graphInfoService.saveBatch(graphInfoArrayList);

    }

    @Override
    public ApartmentDetailVo getDetailApartmentById(Long id) {
        ApartmentInfo apartmentInfo = getById(id);
        if(apartmentInfo ==null)
            return null;


        //note: [查询图片信息] [配套关系] [标签关系] [杂费关系]


        List<GraphVo> graphVoList=graphInfoMapper.selectGraphVoListByApartId(id,ItemType.APARTMENT);

        List<LabelInfo> labelInfoList=apartmentLabelMapper.selectLabelInfoByApartId(id);

        List<FacilityInfo> facilityInfoList = apartmentFacilityMapper.selectFaclicilityInfoListByApartList(id);

        List<FeeValueVo> feeValueVoList= apartmentFeeValueMapper.selectFeeValueVoListByApartId(id);


        //构建新的apartment返回对象
        ApartmentDetailVo adminApartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVo);

        adminApartmentDetailVo.setGraphVoList(graphVoList);
        adminApartmentDetailVo.setLabelInfoList(labelInfoList);
        adminApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        adminApartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return adminApartmentDetailVo;
    }

    @Override
    public boolean removeApartmentById(Long id) {

        Long delApartmentId =id;

        if(roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId,id))>0){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
            //根据公寓id删除公寓信息
            removeById(delApartmentId);
            //根据公寓id删除公寓配套列表
        boolean b = apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                .eq(ApartmentFacility::getApartmentId, delApartmentId));

        apartmentLabelService.remove((new LambdaQueryWrapper<ApartmentLabel>()
                    .eq(ApartmentLabel::getApartmentId, delApartmentId)));
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                    .eq(ApartmentFeeValue::getApartmentId, delApartmentId));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemId, delApartmentId));



        return b;
    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemQuery(IPage<ApartmentItemVo> page,ApartmentQueryVo queryVo) {

        IPage<ApartmentItemVo> list=baseMapper.selectPageApartmentItem(page,queryVo);

        return list;
    }
}




